package com.cmcc.migu.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cmcc.migu.cmmon.util.HTTPUtil;
import com.cmcc.migu.cmmon.util.UIMAESUtil;
import com.cmcc.migu.common.entity.ResultObj;

/** 
 *
 *
 * @Project: UIM-CLIENT
 * @File: RmHttpClientUtil.java 
 * @Date: 2015年5月14日 
 * @Author: 李宏利
 * @Copyright: 版权所有 (C) 2015 中国移动 杭州研发中心. 
 *
 * @注意：本内容仅限于中国移动内部传阅，禁止外泄以及用于其他的商业目的 
 */

public class RmHttpClientUtil {
	private static Logger logger = LoggerFactory.getLogger(RmHttpClientUtil.class);
	
	
	public static final String SERVICE_CODE_CHECK_PHONE_AND_IMEI = "CheckPhoneAndIMEIService";
	public static final String SERVICE_CODE_CHECK_PHONE_AND_IMSI = "CheckPhoneAndIMSIService";
	public static final String SERVICE_CODE_GET_PHONE_WITH_IMEI = "GetPhoneWithIMEIService";
	public static final String SERVICE_CODE_GET_PHONE_WITH_IMSI = "GetPhoneWithIMSIService";
	
	
	/**
	 * 实时消息请求方接口
	 * @param requestUrl		访问地址
	 * @param authCode			统一接口机为每个消息请求方分配的用户名
	 * @param authKey			统一接口机为每个消息请求方分配的密钥
	 * @param serviceCode		开发者要调用的接口编码
	 * @param paramMap			key:IMEI/IMSI		查询串号
	 * 								phoneNo			手机号(只有验证接口必填)
	 * @return					{"errorCode":0,"errorMessage":"","resultMap":{"phoneNo":"13910519000"},"successSign":true}
	 * 							resultMap中获取手机号的返回phoneNo， 校验的返回checkResult
	 */
	public static String execute(String requestUrl, String authCode, String authKey, String serviceCode, Map<String, Object> paramMap){
		Boolean successFlag = true;
		String errMsg = "";
		if(StringUtils.isBlank(requestUrl)){
			successFlag = false;
			errMsg = "输入参数不完整，请传入请求地址";
		}
		if(StringUtils.isBlank(authCode)){
			successFlag = false;
			errMsg = "输入参数不完整，请传入请求者编号";
		}
		if(StringUtils.isBlank(authKey)){
			successFlag = false;
			errMsg = "输入参数不完整，请传入请求者密钥";
		}
		if(StringUtils.isBlank(serviceCode)){
			successFlag = false;
			errMsg = "输入参数不完整， 请传入需要调用的接口编号";
		}
		if(paramMap == null || paramMap.isEmpty()){
			successFlag = false;
			errMsg = "输入参数不完整，请确认请求参数不为空";
		}
		Map<String, Object> otherMap = new HashMap<String, Object>();
		otherMap.put("serviceCode", serviceCode);
		String cipherMessage = "";
		Long start = new Date().getTime();
		if(successFlag){
			try {
				cipherMessage = UIMAESUtil.encrypt(authCode, otherMap, paramMap, authKey);
				logger.info("==============cipherMessage is {}", cipherMessage);
			} catch (Exception e) {
				logger.error("the method execute in class RmHttpClientUtil throw Exception, authCode is {}, serviceCode is {}, paramMap is {}, authKey is {}", authCode, serviceCode, paramMap, authKey, e);
				successFlag = false;
				errMsg = "对请求参数进行加密时出现异常";
			}
		}
		Long end = new Date().getTime();
		logger.info("UIMAESUtil.encrypt use time {}", end-start);
		String result = "";
		if(successFlag){
			try {
				result = HTTPUtil.fetchGetURLContent(requestUrl, "UTF-8", new NameValuePair("json", cipherMessage));
			} catch (Exception e) {
				logger.error("the method execute in class RmHttpClientUtil throw Exception, authCode is {}, serviceCode is {}, paramMap is {}, authKey is {}", authCode, serviceCode, paramMap, authKey, e);
				successFlag = false;
				errMsg = "访问统一接口机失败";
			}
		}
		if(!successFlag){
			ResultObj resultObj = new ResultObj();
			resultObj.setSuccessSign(successFlag);
			resultObj.setErrorCode(100000);
			resultObj.setErrorMessage(errMsg);
			result = JSONObject.fromObject(resultObj).toString();
		}
		return result;
	}

}
